package com.fsnbigdata.web;

import com.fsnbigdata.domain.Document;
import com.fsnbigdata.domain.Organism;
import com.fsnbigdata.domain.ZhDiversityReport;
import com.fsnbigdata.enums.ResultEnum;
import com.fsnbigdata.exception.ParamException;
import com.fsnbigdata.service.api.DiversityReportService;
import com.fsnbigdata.service.api.DocumentService;
import com.fsnbigdata.util.ReflectUtil;
import com.fsnbigdata.util.SortTools;
import io.springlets.web.NotFoundException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.roo.addon.web.mvc.controller.annotations.ControllerType;
import org.springframework.roo.addon.web.mvc.controller.annotations.RooController;
import org.springframework.roo.addon.web.mvc.controller.annotations.responses.json.RooJSON;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.security.Principal;
import java.util.*;
import java.util.stream.Collectors;

@RooController(entity = ZhDiversityReport.class, pathPrefix = "/api", type = ControllerType.COLLECTION)
@RooJSON
@RestController
@RequestMapping(value = "/api/diversityreport", name = "DiversityReportController", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "ZhDiversityReport", description = "自动化报告")
//@RestController
//@RequestMapping("/api/diversityreport")
public class DiversityReportController {
    
    @Autowired
    private DiversityReportService diversityReportService;

    @Autowired
    private DocumentService documentService;
//    @GetMapping("get")
//    public String get(HttpServletRequest request) throws Exception {
//        return diversityReportService.findById(Long.parseLong(request.getParameter("id"))).getContent();
//    }
    
    /**
     * 通过id查询
     *
     * @param request
     * @return
     */
    @GetMapping("findone")
    public ZhDiversityReport findById(HttpServletRequest request) {
        String id = request.getParameter( "id" );
        ZhDiversityReport zhDiversityReport = diversityReportService.findById(Long.parseLong(id));
        zhDiversityReport.setContent( zhDiversityReport.getContent().replaceAll( "styId",id ) );
        diversityReportService.update( zhDiversityReport );
        return diversityReportService.findById(Long.parseLong( id ));
    }
    
    /**
     * 前台自动化报告访问量
     *
     * @param request
     * @return
     */
    @GetMapping("webfindone")
    public ZhDiversityReport findBId(HttpServletRequest request) {
        Long id = Long.parseLong(request.getParameter("id"));
        ZhDiversityReport zhDiversityReport = diversityReportService.findById(id);
        zhDiversityReport.setView_count(zhDiversityReport.getView_count() + 1);
        //下载附件所需的id
        zhDiversityReport.setContent( zhDiversityReport.getContent().replaceAll( "styId",id+"" ) );
        diversityReportService.save(zhDiversityReport);
        return diversityReportService.findById(id);
    }
    
    /**
     * 标题模糊查询
     *
     * @param request
     * @return
     */
    @GetMapping(value = "findByTitle")
    public List<ZhDiversityReport> findByTitle(HttpServletRequest request) {
        return diversityReportService.findAllByTitleLike(request.getParameter("title"));
    }
    
    /**
     * 前台获取地区
     *
     * @param request
     * @return
     */
    @GetMapping(value = "getArea")
    public List getArea(HttpServletRequest request) {
        String city = request.getParameter("city");
        return diversityReportService.getArea(city);
    }
    
    /**
     * 前台通过地区获取报告列表
     *
     * @param request
     * @param pageable
     * @return
     */
    @GetMapping(value = "getPageList")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", defaultValue = "0", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "size", defaultValue = "20", dataType = "int", paramType = "query")
    })
    public List getPageList(HttpServletRequest request, Pageable pageable) {
        String city = request.getParameter("city");
        String county = request.getParameter("county");
        return diversityReportService.getPageList(city, county, pageable);
    }
    
    /**
     * 前台模糊搜索获取报告列表
     *
     * @param request
     * @param pageable
     * @return
     */
    @GetMapping(value = "getList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", defaultValue = "0", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", defaultValue = "20", dataType = "int", paramType = "query")
    })
    public List getList(HttpServletRequest request, Pageable pageable) {
        Pageable pageable1=new PageRequest(pageable.getPageNumber(),pageable.getPageSize(), SortTools.basicSort("desc","update_time"));
        String title = request.getParameter("title");
        return diversityReportService.getList(title, pageable1);
    }

    /**
     * 后台模糊搜索获取报告列表
     *
     * @param request
     * @param pageable
     * @return
     */
    @GetMapping(value = "getList1")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", defaultValue = "0", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", defaultValue = "20", dataType = "int", paramType = "query")
    })
    @PreAuthorize("hasRole('DB_DIVERSITYREPORT')")
    public List getList1(HttpServletRequest request, Authentication authentication, Pageable pageable) {
//        Pageable pageable1=new PageRequest(pageable.getPageNumber(),pageable.getPageSize(), SortTools.basicSort("desc","update_time"));
        List<String> authorities = authentication.getAuthorities().stream().map(it -> it.getAuthority()).collect(Collectors.toList());
        String creator="";
        if(!authorities.contains("ROLE_USER_ADMIN")){
            creator = authentication.getName();
        }
        String title = request.getParameter("title");
        return diversityReportService.getList1(title, creator,pageable);
    }

    /**
     * 查询物种分布的保护区
     * @return
     */
    @GetMapping("getProtection")
    public List getProtection(){
        return diversityReportService.getProtection();
    }
    
    
    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping("findAll")
    public List<ZhDiversityReport> findAll() {
        return diversityReportService.findAll();
    }
    
    /**
     * 新增
     *
     * @param zhDiversityReport
     * @param result
     * @return
     */
    @PostMapping(value = "save")
    @PreAuthorize("hasRole('DB_DIVERSITYREPORT')")
    @ApiOperation(value = "",notes = "save")
    public ResponseEntity<?> save(@Valid @RequestBody ZhDiversityReport zhDiversityReport, BindingResult result, Principal principal) throws Exception {
        if (zhDiversityReport.getId() != null) {
            return ResponseEntity.status(HttpStatus.CONFLICT).build();
        }
        if (result.hasErrors()) {
            throw new ParamException(ResultEnum.PARAM_ERROR.getCode(),
                    result.getFieldError().getDefaultMessage());
        }

        String city = "";
        if(zhDiversityReport.getProvince()!=null&&!("".equals( zhDiversityReport.getProvince() ))){
            city = "省";
        }
        if(zhDiversityReport.getCity()!=null&&!("".equals( zhDiversityReport.getCity() ))){
            city = "市";
        }
        if(zhDiversityReport.getCounty()!=null&&!("".equals( zhDiversityReport.getCounty() ))){
            if(zhDiversityReport.getCounty().indexOf( "县" )!=-1){
                city = "县";
            }else if(zhDiversityReport.getCounty().indexOf( "保护区" )!=-1){
                city = "保护区";
            }else {
                city = "区";
            }
        }
        zhDiversityReport.setMicrobe_heredity_protect( city );
        zhDiversityReport.setAuthor(principal.getName());
        zhDiversityReport.setCreate_time(new Date());
        zhDiversityReport.setUpdate_time(new Date());
        //添加content内容
        zhDiversityReport.setContent(testIndex(zhDiversityReport));
        Set<Document> toSaveDocs = zhDiversityReport.getDocs();
        zhDiversityReport.setDocs(new HashSet<>());
        ZhDiversityReport newZhDiversityReport = diversityReportService.save(zhDiversityReport);

        Set<Long> docIds = toSaveDocs.stream().map(doc -> {
            if(doc.getId() == null) {
                doc.setCreator(principal.getName());
                documentService.save(doc);
                return doc.getId();
            }else{
                return doc.getId();
            }
        }).collect(Collectors.toSet());

        diversityReportService.setDocs(newZhDiversityReport, docIds);


        Class<?> clazz = DiversityReportController.class;
        Method method = clazz.getMethod("save", ReflectUtil.getPeremeterClass(clazz, "save"));
        ReflectUtil.setAnnotationOn(method,"新增生物多样性自动动画报告，标题："+newZhDiversityReport.getTitle());

        return ResponseEntity.ok(newZhDiversityReport);
    }

    /**
     * 修改
     *
     * @param zhDiversityReport
     * @param result
     * @return
     */
    @PutMapping(value = "update")
    @PreAuthorize("hasRole('DB_DIVERSITYREPORT')")
    @ApiOperation(value = "",notes = "update")
    public ResponseEntity<?> update( @Valid @RequestBody ZhDiversityReport zhDiversityReport, BindingResult result, Principal principal) throws Exception {
        if (result.hasErrors()) {
            throw new ParamException(ResultEnum.PARAM_ERROR.getCode(),
                    result.getFieldError().getDefaultMessage());
        }

        String city = "";
        if(zhDiversityReport.getProvince()!=null&&!("".equals( zhDiversityReport.getProvince() ))){
            city = "省";
        }
        if(zhDiversityReport.getCity()!=null&&!("".equals( zhDiversityReport.getCity() ))){
            city = "市";
        }
        if(zhDiversityReport.getCounty()!=null&&!("".equals( zhDiversityReport.getCounty() ))){
            if(zhDiversityReport.getCounty().indexOf( "县" )!=-1){
                city = "县";
            }else if(zhDiversityReport.getCounty().indexOf( "保护区" )!=-1){
                city = "保护区";
            }else {
                city = "区";
            }
        }
        if(zhDiversityReport.getIs_protection()==1){
            zhDiversityReport.setCity( "保护区" );
            zhDiversityReport.setProvince( "" );
        }
        ZhDiversityReport storedZhDiversityReport = diversityReportService.findById(zhDiversityReport.getId());
        zhDiversityReport.setMicrobe_heredity_protect( city );
        zhDiversityReport.setUpdate_time(new Date());
        zhDiversityReport.setAuthor(principal.getName());
        //添加content内容
        zhDiversityReport.setContent(testIndex(zhDiversityReport));
        Set<Document> toSaveDocs = zhDiversityReport.getDocs();
        zhDiversityReport.setDocs(storedZhDiversityReport.getDocs());

        String title = diversityReportService.findTitleById(zhDiversityReport.getId());
        ZhDiversityReport newZhDiversityReport =  diversityReportService.save(zhDiversityReport);

        Set<Long> docIds = toSaveDocs.stream().map(doc -> {
            if(doc.getId() == null) {
                doc.setCreator(principal.getName());
                documentService.save(doc);
                return doc.getId();
            }else{
                return doc.getId();
            }
        }).collect(Collectors.toSet());

        diversityReportService.setDocs(newZhDiversityReport, docIds);


        Class<?> clazz = DiversityReportController.class;
        Method method = clazz.getMethod("update", ReflectUtil.getPeremeterClass(clazz, "update"));
        ReflectUtil.setAnnotationOn(method,"更新生物多样性自动化报告，标题："+title);

        return ResponseEntity.ok(newZhDiversityReport);
    }
    
    /**
     * 通过id删除单条记录
     *
     * @param request
     * @return
     */
    @DeleteMapping(name = "delete", value = "deleteone")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "", paramType = "path", dataType = "int", required = true)
    })
    @PreAuthorize("hasRole('DB_DIVERSITYREPORT')")
    @ApiOperation(value = "",notes = "delete")
    public ResponseEntity<?> delete(HttpServletRequest request) throws Exception {
        String id = request.getParameter("id");
        if(id==null||"".equals(id)){
            ResponseEntity.badRequest().body("沒有获取到参数");
        }
        Long zhId = 0L;
        try {
         zhId= Long.parseLong(id);
        }catch (Exception e){
            System.out.println(e.getMessage());
            ResponseEntity.badRequest().body("参数格式不对");
        }

        String title = diversityReportService.findTitleById(zhId);
        diversityReportService.deleteOne(zhId);

        Class<?> clazz = DiversityReportController.class;
        Method method = clazz.getMethod("delete", ReflectUtil.getPeremeterClass(clazz, "delete"));
        ReflectUtil.setAnnotationOn(method,"删除生物多样性自动化报告，标题："+title);

        return ResponseEntity.ok().build();
    }
    
    /**
     * 删除多条记录
     *
     * @param ids
     * @return
     */
    @DeleteMapping(name = "deleteMany", value = "deleteMany")
    @PreAuthorize("hasRole('DB_DIVERSITYREPORT')")
    public ResponseEntity<?> deleteMany(@PathVariable("ids") Collection<Long> ids) {
        diversityReportService.deleteMany(ids);
        return ResponseEntity.ok().build();
    }
    
    /**
     * 修改发布状态
     *
     * @param request
     * @return
     */
    @GetMapping("changeStatus")
    public ZhDiversityReport changeStatus(HttpServletRequest request) {
        ZhDiversityReport zhDiversityReport = diversityReportService.findById(Long.parseLong(request.getParameter("id")));
        if(zhDiversityReport.getStatus()==0){
            zhDiversityReport.setStatus(1);
        }else {
            zhDiversityReport.setStatus( 0 );
        }
        zhDiversityReport.setUpdate_time( new Date(  ) );
        return diversityReportService.save(zhDiversityReport);
    }
    
    /**
     * 修改状态为 0
     *
     * @param request
     * @return
     */
    @GetMapping("changeStatusZero")
    public ZhDiversityReport changeStatusTo(HttpServletRequest request) {
        ZhDiversityReport zhDiversityReport = diversityReportService.findById(Long.parseLong(request.getParameter("id")));
        zhDiversityReport.setStatus(0);
        return diversityReportService.save(zhDiversityReport);
    }
    
    
    /**
     * 自动化报告占位符替换
     * chen quan zhou
     *
     * @param zhDiversityReport
     * @return
     */
    public String testIndex(ZhDiversityReport zhDiversityReport) {
        
        String txt = null;
        try {
//            txt = diversityReportService.getContent(zhDiversityReport.getProvince(), zhDiversityReport.getCity(), zhDiversityReport.getCounty(), zhDiversityReport.getWetland_type4(), zhDiversityReport.getWetland_type5());
            txt = diversityReportService.getContent(zhDiversityReport);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        //数据库字段占位符替换
        
        StringBuffer stringBuffer = new StringBuffer();
        StringBuffer stringBuffer1 = new StringBuffer();

        if(zhDiversityReport.getIs_protection()==0){
            if (zhDiversityReport.getProvince() != null && !"".equals(zhDiversityReport.getProvince())) {
                stringBuffer.append(zhDiversityReport.getProvince());
                if (zhDiversityReport.getCity() != null && !"".equals(zhDiversityReport.getCity())) {
                    stringBuffer.append(zhDiversityReport.getCity());
                    if (zhDiversityReport.getCounty() != null && !"".equals(zhDiversityReport.getCounty())) {
                        stringBuffer.append(zhDiversityReport.getCounty());
                    }
                }
                txt = txt.replaceAll("styarea", stringBuffer + "");
            } else if (zhDiversityReport.getCity() != null && !"".equals(zhDiversityReport.getCity())) {
                stringBuffer1.append(zhDiversityReport.getCity());
                if (zhDiversityReport.getCounty() != null && !"".equals(zhDiversityReport.getCounty())) {
                    stringBuffer1.append(zhDiversityReport.getCounty());
                }
                txt = txt.replaceAll("styarea", stringBuffer1 + "");
            } else if (zhDiversityReport.getCounty() != null && !"".equals(zhDiversityReport.getCounty())) {
                txt = txt.replaceAll("styarea", zhDiversityReport.getCounty());
            }
        }else {
            txt = txt.replaceAll("styarea",  zhDiversityReport.getCounty());
        }

        
        if (zhDiversityReport.getNatural_profiles() != null && !"".equals(zhDiversityReport.getNatural_profiles())) {
            txt = txt.replaceAll("stynatural_profiles", zhDiversityReport.getNatural_profiles());
        } else {
            txt = txt.replaceAll("stynatural_profiles", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getAdministrative_division() != null && !"".equals(zhDiversityReport.getAdministrative_division())) {
            txt = txt.replaceAll("styadministrative_division", zhDiversityReport.getAdministrative_division());
        } else {
            txt = txt.replaceAll("styadministrative_division", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getPopulation_and_nationality() != null && !"".equals(zhDiversityReport.getPopulation_and_nationality())) {
            txt = txt.replaceAll("stypopulation_and_nationality", zhDiversityReport.getPopulation_and_nationality());
        } else {
            txt = txt.replaceAll("stypopulation_and_nationality", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getSocial_economy() != null && !"".equals(zhDiversityReport.getSocial_economy())) {
            txt = txt.replaceAll("stysocial_economy", zhDiversityReport.getSocial_economy());
        } else {
            txt = txt.replaceAll("stysocial_economy", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getLayer() != null && !"".equals(zhDiversityReport.getLayer())) {
            txt = txt.replaceAll("stylayer", zhDiversityReport.getLayer());
        } else {
            txt = txt.replaceAll("stylayer", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getTerrain_structure() != null && !"".equals(zhDiversityReport.getTerrain_structure())) {
            txt = txt.replaceAll("styterrain_structure", zhDiversityReport.getTerrain_structure());
        } else {
            txt = txt.replaceAll("styterrain_structure", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getMountains() != null && !"".equals(zhDiversityReport.getMountains())) {
            txt = txt.replaceAll("stymountains", zhDiversityReport.getMountains());
        } else {
            txt = txt.replaceAll("stymountains", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getWeather() != null && !"".equals(zhDiversityReport.getWeather())) {
            txt = txt.replaceAll("styweather", zhDiversityReport.getWeather());
        } else {
            txt = txt.replaceAll("styweather", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getRivers_river_systems_basins() != null && !"".equals(zhDiversityReport.getRivers_river_systems_basins())) {
            txt = txt.replaceAll("styrivers_river_systems_basins", zhDiversityReport.getRivers_river_systems_basins());
        } else {
            txt = txt.replaceAll("styrivers_river_systems_basins", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getSoil() != null && !"".equals(zhDiversityReport.getSoil())) {
            txt = txt.replaceAll("stysoil", zhDiversityReport.getSoil());
        } else {
            txt = txt.replaceAll("stysoil", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getForest_ecosystem() != null && !"".equals(zhDiversityReport.getForest_ecosystem())) {
            txt = txt.replaceAll("styforest_ecosystem", zhDiversityReport.getForest_ecosystem());
        } else {
            txt = txt.replaceAll("styforest_ecosystem", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getForest_type() != null && !"".equals(zhDiversityReport.getForest_type())) {
            txt = txt.replaceAll("styforest_type", zhDiversityReport.getForest_type());
        } else {
            txt = txt.replaceAll("styforest_type", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getPlantage_input1() != null && !"".equals(zhDiversityReport.getPlantage_input1())) {
            txt = txt.replaceAll("styplantage_input1", zhDiversityReport.getPlantage_input1());
        } else {
            txt = txt.replaceAll("styplantage_input1", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getPlantage_input2() != null && !"".equals(zhDiversityReport.getPlantage_input2())) {
            txt = txt.replaceAll("styplantage_input2", zhDiversityReport.getPlantage_input2());
        } else {
            txt = txt.replaceAll("styplantage_input2", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getBasic_characteristics() != null && !"".equals(zhDiversityReport.getBasic_characteristics())) {
            txt = txt.replaceAll("stybasic_characteristics", zhDiversityReport.getBasic_characteristics());
        } else {
            txt = txt.replaceAll("stybasic_characteristics", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getForestry_industry() != null && !"".equals(zhDiversityReport.getForestry_industry())) {
            txt = txt.replaceAll("styforestry_industry", zhDiversityReport.getForestry_industry());
        } else {
            txt = txt.replaceAll("styforestry_industry", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getWetland_type1() != null && !"".equals(zhDiversityReport.getWetland_type1())) {
            txt = txt.replaceAll("stywetland_type1", zhDiversityReport.getWetland_type1());
        } else {
            txt = txt.replaceAll("stywetland_type1", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getWetland_type2() != null && !"".equals(zhDiversityReport.getWetland_type2())) {
            txt = txt.replaceAll("stywetland_type2", zhDiversityReport.getWetland_type2());
        } else {
            txt = txt.replaceAll("stywetland_type2", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getWetland_type3() != null && !"".equals(zhDiversityReport.getWetland_type3())) {
            txt = txt.replaceAll("stywetland_type3", zhDiversityReport.getWetland_type3());
        } else {
            txt = txt.replaceAll("stywetland_type3", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getWetland_type4() != null && !"".equals(zhDiversityReport.getWetland_type4())) {
            txt = txt.replaceAll("stywetland_type4", zhDiversityReport.getWetland_type4());
        } else {
            txt = txt.replaceAll("stywetland_type4", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getWetland_type6() != null && !"".equals(zhDiversityReport.getWetland_type6())) {
            txt = txt.replaceAll("stywetland_type6", zhDiversityReport.getWetland_type6());
        } else {
            txt = txt.replaceAll("stywetland_type6", "此处暂无详细内容！");
        }

        if (zhDiversityReport.getWetland_type7() != null && !"".equals(zhDiversityReport.getWetland_type7())) {
            txt = txt.replaceAll("stywetland_type7", zhDiversityReport.getWetland_type7());
        } else {
            txt = txt.replaceAll("stywetland_type7", "此处暂无详细内容！");
        }
        if (zhDiversityReport.getWetland_type8() != null && !"".equals(zhDiversityReport.getWetland_type8())) {
            txt = txt.replaceAll("stywetland_type8", zhDiversityReport.getWetland_type8());
        } else {
            txt = txt.replaceAll("stywetland_type8", "此处暂无详细内容！");
        }

        if (zhDiversityReport.getHole() != null && !"".equals(zhDiversityReport.getHole())) {
            txt = txt.replaceAll("styhole", zhDiversityReport.getHole());
        } else {
            txt = txt.replaceAll("styhole", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getRiverine_wetland1() != null && !"".equals(zhDiversityReport.getRiverine_wetland1())) {
            txt = txt.replaceAll("styriverine_wetland1", zhDiversityReport.getRiverine_wetland1());
        } else {
            txt = txt.replaceAll("styriverine_wetland1", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getRiverine_wetland2() != null && !"".equals(zhDiversityReport.getRiverine_wetland2())) {
            txt = txt.replaceAll("styriverine_wetland2", zhDiversityReport.getRiverine_wetland2());
        } else {
            txt = txt.replaceAll("styriverine_wetland2", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getRiverine_wetland3() != null && !"".equals(zhDiversityReport.getRiverine_wetland3())) {
            txt = txt.replaceAll("styriverine_wetland3", zhDiversityReport.getRiverine_wetland3());
        } else {
            txt = txt.replaceAll("styriverine_wetland3", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getRiverine_wetland4() != null && !"".equals(zhDiversityReport.getRiverine_wetland4())) {
            txt = txt.replaceAll("styriverine_wetland4", zhDiversityReport.getRiverine_wetland4());
        } else {
            txt = txt.replaceAll("styriverine_wetland4", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getAgroecological_system() != null && !"".equals(zhDiversityReport.getAgroecological_system())) {
            txt = txt.replaceAll("styagroecological_system", zhDiversityReport.getAgroecological_system());
        } else {
            txt = txt.replaceAll("styagroecological_system", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getMicrobial_diversity_one() != null && !"".equals(zhDiversityReport.getMicrobial_diversity_one())) {
            txt = txt.replaceAll("stymicrobial_diversity_one", zhDiversityReport.getMicrobial_diversity_one());
        } else {
            txt = txt.replaceAll("stymicrobial_diversity_one", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getMicrobial_diversity_two() != null && !"".equals(zhDiversityReport.getMicrobial_diversity_two())) {
            txt = txt.replaceAll("stymicrobial_diversity_two", zhDiversityReport.getMicrobial_diversity_two());
        } else {
            txt = txt.replaceAll("stymicrobial_diversity_two", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getPlant_animal_resources() != null && !"".equals(zhDiversityReport.getPlant_animal_resources())) {
            txt = txt.replaceAll("styplant_animal_resources", zhDiversityReport.getPlant_animal_resources());
        } else {
            txt = txt.replaceAll("styplant_animal_resources", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getPlant_genetic_resources() != null && !"".equals(zhDiversityReport.getPlant_genetic_resources())) {
            txt = txt.replaceAll("styplant_genetic_resources", zhDiversityReport.getPlant_genetic_resources());
        } else {
            txt = txt.replaceAll("styplant_genetic_resources", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getMicrobial_genetic_resources() != null && !"".equals(zhDiversityReport.getMicrobial_genetic_resources())) {
            txt = txt.replaceAll("stymicrobial_genetic_resources", zhDiversityReport.getMicrobial_genetic_resources());
        } else {
            txt = txt.replaceAll("stymicrobial_genetic_resources", "此处暂无详细内容！");
        }

        if (zhDiversityReport.getMileage() != null && !"".equals(zhDiversityReport.getMileage())) {
            txt = txt.replaceAll("stymileage", zhDiversityReport.getMileage());
        } else {
            txt = txt.replaceAll("stymileage", "此处暂无详细内容！");
        }

        if (zhDiversityReport.getHighway_density() != null && !"".equals(zhDiversityReport.getHighway_density())) {
            txt = txt.replaceAll("styhighway_density", zhDiversityReport.getHighway_density());
        } else {
            txt = txt.replaceAll("styhighway_density", "此处暂无详细内容！");
        }

        if (zhDiversityReport.getEcological_pollution() != null && !"".equals(zhDiversityReport.getEcological_pollution())) {
            txt = txt.replaceAll("styecological_pollution", zhDiversityReport.getEcological_pollution());
        } else {
            txt = txt.replaceAll("styecological_pollution", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getHabitat_degradation() != null && !"".equals(zhDiversityReport.getHabitat_degradation())) {
            txt = txt.replaceAll("styhabitat_degradation", zhDiversityReport.getHabitat_degradation());
        } else {
            txt = txt.replaceAll("styhabitat_degradation", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getDevelopment_and_utilization() != null && !"".equals(zhDiversityReport.getDevelopment_and_utilization())) {
            txt = txt.replaceAll("stydevelopment_and_utilization", zhDiversityReport.getDevelopment_and_utilization());
        } else {
            txt = txt.replaceAll("stydevelopment_and_utilization", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getSpecies_invasion() != null && !"".equals(zhDiversityReport.getSpecies_invasion())) {
            txt = txt.replaceAll("styspecies_invasion", zhDiversityReport.getSpecies_invasion());
        } else {
            txt = txt.replaceAll("styspecies_invasion", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getEnvironmental_pollution() != null && !"".equals(zhDiversityReport.getEnvironmental_pollution())) {
            txt = txt.replaceAll("styenvironmental_pollution", zhDiversityReport.getEnvironmental_pollution());
        } else {
            txt = txt.replaceAll("styenvironmental_pollution", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getLaws_and_regulations() != null && !"".equals(zhDiversityReport.getLaws_and_regulations())) {
            txt = txt.replaceAll("stylaws_and_regulations", zhDiversityReport.getLaws_and_regulations());
        } else {
            txt = txt.replaceAll("stylaws_and_regulations", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getNatural_reserve() != null && !"".equals(zhDiversityReport.getNatural_reserve())) {
            txt = txt.replaceAll("stynatural_reserve", zhDiversityReport.getNatural_reserve());
        } else {
            txt = txt.replaceAll("stynatural_reserve", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getForest_park() != null && !"".equals(zhDiversityReport.getForest_park())) {
            txt = txt.replaceAll("styforest_park", zhDiversityReport.getForest_park());
        } else {
            txt = txt.replaceAll("styforest_park", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getWetland_park() != null && !"".equals(zhDiversityReport.getWetland_park())) {
            txt = txt.replaceAll("stywetland_park", zhDiversityReport.getWetland_park());
        } else {
            txt = txt.replaceAll("stywetland_park", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getTourist_attraction() != null && !"".equals(zhDiversityReport.getTourist_attraction())) {
            txt = txt.replaceAll("stytourist_attraction", zhDiversityReport.getTourist_attraction());
        } else {
            txt = txt.replaceAll("stytourist_attraction", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getEx_situ_conservation() != null && !"".equals(zhDiversityReport.getEx_situ_conservation())) {
            txt = txt.replaceAll("styex_situ_conservation", zhDiversityReport.getEx_situ_conservation());
        } else {
            txt = txt.replaceAll("styex_situ_conservation", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getNormative_documents() != null && !"".equals(zhDiversityReport.getNormative_documents())) {
            txt = txt.replaceAll("stynormative_documents", zhDiversityReport.getNormative_documents());
        } else {
            txt = txt.replaceAll("stynormative_documents", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getDescription_diversity_track() != null && !"".equals(zhDiversityReport.getDescription_diversity_track())) {
            txt = txt.replaceAll("stydescription_diversity_track", zhDiversityReport.getDescription_diversity_track());
        } else {
            txt = txt.replaceAll("stydescription_diversity_track", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getDescription_diversity_hotspots1() != null && !"".equals(zhDiversityReport.getDescription_diversity_hotspots1())) {
            txt = txt.replaceAll("stydescription_diversity_hotspots1", zhDiversityReport.getDescription_diversity_hotspots1());
        } else {
            txt = txt.replaceAll("stydescription_diversity_hotspots1", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getDescription_diversity_hotspots2() != null && !"".equals(zhDiversityReport.getDescription_diversity_hotspots2())) {
            txt = txt.replaceAll("stydescription_diversity_hotspots2", zhDiversityReport.getDescription_diversity_hotspots2());
        } else {
            txt = txt.replaceAll("stydescription_diversity_hotspots2", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getDescription_diversity_hotspots3() != null && !"".equals(zhDiversityReport.getDescription_diversity_hotspots3())) {
            txt = txt.replaceAll("stydescription_diversity_hotspots3", zhDiversityReport.getDescription_diversity_hotspots3());
        } else {
            txt = txt.replaceAll("stydescription_diversity_hotspots3", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getDescription_diversity_hotspots4() != null && !"".equals(zhDiversityReport.getDescription_diversity_hotspots4())) {
            txt = txt.replaceAll("stydescription_diversity_hotspots4", zhDiversityReport.getDescription_diversity_hotspots4());
        } else {
            txt = txt.replaceAll("stydescription_diversity_hotspots4", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getDescription_diversity_hotspots5() != null && !"".equals(zhDiversityReport.getDescription_diversity_hotspots5())) {
            txt = txt.replaceAll("stydescription_diversity_hotspots5", zhDiversityReport.getDescription_diversity_hotspots5());
        } else {
            txt = txt.replaceAll("stydescription_diversity_hotspots5", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getDescription_pollutant_emission() != null && !"".equals(zhDiversityReport.getDescription_pollutant_emission())) {
            txt = txt.replaceAll("stydescription_pollutant_emission", zhDiversityReport.getDescription_pollutant_emission());
        } else {
            txt = txt.replaceAll("stydescription_pollutant_emission", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getDescription_study() != null && !"".equals(zhDiversityReport.getDescription_study())) {
            txt = txt.replaceAll("stydescription_study", zhDiversityReport.getDescription_study());
        } else {
            txt = txt.replaceAll("stydescription_study", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getMicrobe_heredity_protect() != null && !"".equals(zhDiversityReport.getMicrobe_heredity_protect())) {
            txt = txt.replaceAll("stymicrobe_heredity_protect", zhDiversityReport.getMicrobe_heredity_protect());
        } else {
            txt = txt.replaceAll("stymicrobe_heredity_protect", "此处暂无详细内容！");
        }
        
        if (zhDiversityReport.getReply_environmental_problems() != null && !"".equals(zhDiversityReport.getReply_environmental_problems())) {
            txt = txt.replaceAll("styreply_environmental_problems", zhDiversityReport.getReply_environmental_problems());
        } else {
            txt = txt.replaceAll("styreply_environmental_problems", "此处暂无详细内容！");
        }

        if (zhDiversityReport.getDocs() != null && zhDiversityReport.getDocs().size()>0) {
            StringBuffer doc = new StringBuffer();
            Set<Document> documents = zhDiversityReport.getDocs();
            //这里是对文献进行整理现实start
            for (Document document:documents){
                if (document.getName().indexOf(document.getName())!=-1){
                    //拼接
                    doc.append(document.getAuthor()+" "+ document.getName()+"<br/>");
                }else {
                    //表示名字中含有作者
                    doc.append(document.getName()+"<br/>");

                }
            }
            //这里是对文献进行整理现实end
            txt = txt.replaceAll("styreference_documentation", doc.toString());
        } else {
            txt = txt.replaceAll("styreference_documentation", "此处暂无详细内容！");
        }
        return txt;
    }
    
    
    @GetMapping("indexRedTest")
    public String indexRedTest() throws Exception {
        
        return diversityReportService.testWords();
    }
}
